/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    6. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    6. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "los_base.h"
#include "los_task.h"
#include "los_hwi.h"
#include "los_typedef.h"
#include "pcrambox.h"
#include "los_api_tlsf.h"
#include "los_inspect_entry.h"
#include "ff.h"
#include "diskio.h"
#include "csg_sdk_hal.h"
#include "rx8803.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */
static FATFS fs_flash;			/* FatFS object*/
static FIL fnew;			/* File object */
static FIL fp;
static FRESULT res_flash;              /*File result*/
static FILINFO fil_info;
static UINT fnum;  
static BYTE ReadBuffer[32]={0};        /* File read buffer */
static BYTE WriteBuffer[] =             /* File write buffer */
"abcdef0123456789";
static int DEFAULT_DELAY_CNT = 100;

UINT32 Example_flash_FatFS_fil_read(VOID)
{
    UINT32 uwRet;
    printf("\r\n--------4.4 flash_FatFS_fil_read test start!--------\r\n");
    res_flash = f_mount(&fs_flash,"0:",1);

    /* Formatting test */
    //res_flash == FR_NO_FILESYSTEM
    // if(res_flash == FR_NO_FILESYSTEM)
    if(1)
    {
        printf("Formatting start...\r\n");
        res_flash=f_mkfs("0:",1,0);

        printf("FatFs structure:\r\n");
//
//        if(disk_read(1, ReadBuffer, 0, 1) == RES_OK){
//            for(int i = 0; i < 1024; i++)
//                printf("%d  ", ReadBuffer[i]);
//            printf("=======\r\n");
//        }

        if(res_flash == FR_OK)
        {
            printf("Formatting success!\r\n");

            res_flash = f_mount(NULL,"0:",1);
            if(res_flash == FR_OK)
            {
                printf("f_mount(NULL,\"0:\",1) success!\r\n");
            }
            else
            {
                printf("f_mount(NULL,\"0:\",1) failed! res_flash is %d.\r\n",res_flash);
            }

            res_flash = f_mount(&fs_flash,"0:",1);
            if(res_flash == FR_OK)
            {
                printf("f_mount(&fs_flash,\"0:\",1) success!\r\n");
            }
            else
            {
                printf("f_mount(&fs_flash,\"0:\",1) failed! res_flash is %d.\r\n",res_flash);
            }
        }
        else
        {
            printf("Formatting failed!\r\n");

            while(1);
        }
    }
    else if(res_flash!=FR_OK)
    {
        printf("File system mount failed!\r\n");

        while(1);
    }
    else
    {
      printf("File system mount success!\r\n");
    }
    res_flash=f_opendir(&fnew,"0:TEST_DIR");
    if(res_flash!=FR_OK)
    {
        printf("'0:TEST_DIR' f_opendir failed! res_flash is %d.\r\n",res_flash);
        res_flash=f_mkdir("0:TEST_DIR");
        if(res_flash==FR_OK)
        {
            printf("'0:TEST_DIR' f_mkdir success!\r\n");
        }
        else
        {
            printf("'0:TEST_DIR' f_mkdir failed! res_flash is %d.\r\n",res_flash);
        }
    }
    else
    {
        printf("'0:TEST_DIR' f_opendir success!\r\n");
    }
    /* Open file, if not exist then create it */
    printf("\r\n0:TEST_DIR/test.scv create start!\r\n");
    res_flash = f_open(&fnew, "0:TEST_DIR/test.scv",FA_CREATE_ALWAYS | FA_WRITE | FA_READ );
    if ( res_flash == FR_OK )
    {
        printf("\r\n0:TEST_DIR/test.scv create success!\r\n");
        printf("\r\nwrite 'test.scv' !\r\n");
        res_flash=f_write(&fnew,WriteBuffer,sizeof(WriteBuffer),&fnum);
        if(res_flash==FR_OK)
        {
        	printf("Writting success, byte number is: %d\n",fnum);
            printf("write addr is '0x0', The Writting data is:'%s'\r\n",WriteBuffer);
        }
        else
        {
        	printf("!!Writting failed:(%d)\n",res_flash);
        }

        res_flash = f_lseek (&fnew , 0x0);
		if(res_flash==FR_OK)
		{
			printf("addr pointer move to '0x0' success!\r\n");
		}
		else
		{
			printf("addr pointer move to '0x0' failed!\r\n");
		}
        memset(ReadBuffer,0,32);
		res_flash = f_read(&fnew, ReadBuffer, sizeof(WriteBuffer), &fnum);
		if(res_flash==FR_OK)
		{
			printf("Reading success, byte number is: %d\r\n",fnum);
			printf("read addr is '0x0', The reading data is:'%s'\r\n", ReadBuffer);
		}
		else
		{
			printf("Reading failed:(%d)\n",res_flash);
		}
        res_flash = f_lseek (&fnew , 0x4);
		if(res_flash==FR_OK)
		{
			printf("addr pointer move to '0x4' success!\r\n");
		}
		else
		{
			printf("addr pointer move to '0x4' failed!\r\n");
		}
        memset(ReadBuffer,0,32);
		res_flash = f_read(&fnew, ReadBuffer, 5, &fnum);
		if(res_flash==FR_OK)
		{
			printf("Reading success, byte number is: %d\r\n",fnum);
			printf("read addr is '0x4', The reading data is:'%s'\r\n", ReadBuffer);
		}
		else
		{
			printf("Reading failed:(%d)\n",res_flash);
		}
        f_close(&fnew);
    }
    else
    {
        printf("result is %d !!Open/Create file failed.\r\n",res_flash);
    }
    
    printf("7. pcram_FatFS_fil_read test success!\r\n");
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
